#include "stm32f10x.h"
#include "delay.h"
#include "debug.h"
uint16_t count = 0;
void pwm_timer_init(void);
void capture_timer_init(void);
double capture_get_Cycle(void);
double capture_get_Freq(void);
int main(void)
{
    uart_init();
    pwm_timer_init();
    capture_timer_init();
    double t, f;
    while (1)
    {
        t = capture_get_Cycle();
        f = capture_get_Freq();
        printf("t=%.4fms, f=%.4fHz\r\n", t, f);
        delay_s(1);
    }
}
void pwm_timer_init(void)
{
    RCC->APB1ENR |= RCC_APB1ENR_TIM5EN;
    RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
    GPIOA->CRL |= (GPIO_CRL_CNF1_1 | GPIO_CRL_MODE1);
    GPIOA->CRL &= ~GPIO_CRL_CNF1_0;
    TIM5->PSC = 7200 - 1;
    TIM5->ARR = 100 - 1;
    TIM5->CR1 &= ~TIM_CR1_DIR;
    TIM5->CCR2 = 50;
    TIM5->CCMR1 &= ~TIM_CCMR1_CC2S;
    TIM5->CCMR1 |= TIM_CCMR1_OC2M_2;
    TIM5->CCMR1 |= TIM_CCMR1_OC2M_1;
    TIM5->CCMR1 &= ~TIM_CCMR1_OC2M_0;
    TIM5->CCER |= TIM_CCER_CC2E;
    TIM5->CCER &= ~TIM_CCER_CC2P;
    TIM5->CR1 |= TIM_CR1_CEN;
}
void capture_timer_init(void)
{
    RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;
    RCC->APB2ENR |= RCC_APB2ENR_IOPBEN;
    GPIOB->CRL &= ~(GPIO_CRL_CNF6_1 | GPIO_CRL_MODE6);
    GPIOB->CRL |= GPIO_CRL_CNF6_0;
    TIM4->PSC = 72 - 1;
    TIM4->ARR = 65535;
    TIM4->CR1 &= ~TIM_CR1_DIR;
    TIM4->CR2 &= ~TIM_CR2_TI1S;
    TIM4->CCMR1 &= ~TIM_CCMR1_IC1F;
    TIM4->CCER &= ~TIM_CCER_CC1P;
    TIM4->CCMR1 &= ~TIM_CCMR1_CC1S_1;
    TIM4->CCMR1 |= TIM_CCMR1_CC1S_0;
    TIM4->CCMR1 &= ~TIM_CCMR1_IC1PSC;
    TIM4->CCER |= TIM_CCER_CC1E;
    TIM4->DIER |= TIM_DIER_CC1IE;
    NVIC_SetPriorityGrouping(3);
    NVIC_SetPriority(TIM4_IRQn, 3);
    NVIC_EnableIRQ(TIM4_IRQn);
    TIM4->CR1 |= TIM_CR1_CEN;
}
uint8_t raiseEdgeCount = 0;
uint16_t t = 0;
void TIM4_IRQHandler(void)
{
    if (TIM4->SR & TIM_SR_CC1IF)
    {
        TIM4->SR &= ~TIM_SR_CC1IF;
        raiseEdgeCount++;
        if (raiseEdgeCount == 1)
        {
            TIM4->CNT = 0;
        }
        else if (raiseEdgeCount == 2)
        {
            t = TIM4->CCR1;
            raiseEdgeCount = 0;
        }
    }
}
double capture_get_Cycle(void)
{
    return t / 1000.0;
}
double capture_get_Freq(void)
{
    return 1000000 / t;
}
/****************************************   hal  ****************************************/
// uint8_t raiseEdgeCount = 0;
// uint16_t t = 0;
// void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
// {
//     if (htim->Instance == TIM4)
//     {
//         if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1)
//         {
//             raiseEdgeCount++;
//             if (raiseEdgeCount == 1)
//             {
//                 __HAL_TIM_SetCounter(htim, 0);
//             }
//             else if (raiseEdgeCount == 2)
//             {
//                 t = __HAL_TIM_GetCompare(htim, TIM_CHANNEL_1);
//                 raiseEdgeCount = 0;
//             }
//         }
//     }
// }
// double get_Cycle(void)
// {
//     return t / 1000.0;
// }
// double get_Freq(void)
// {
//     return 1000000 / t;
// }
// void main(void)
// {
//     HAL_TIM_PWM_Start(&htim5, TIM_CHANNEL_2);
//     HAL_TIM_IC_Start_IT(&htim4, TIM_CHANNEL_1);
//     double t, f;
//     while (1)
//     {
//         t = get_Cycle();
//         f = get_Freq();
//         printf("t=%.4fms, f=%.4fHz\r\n", t, f);
//         HAL_Delay(1000);
//     }
// }
